package com.tojabo.sound.ogg;

import java.util.LinkedList;

import com.tojabo.entity.DoReMiFa;

public class SinglePlayerManager {

	private static SinglePlayerManager manager;
	
	private LinkedList<NewOggPlayer> playerQueue = new LinkedList<NewOggPlayer>();
	private LinkedList<DoReMiFa> soundQueue = new LinkedList<DoReMiFa>();
	
	private int soundIndex=0;
	
	private SinglePlayerManager(){
		initData(20);
	}
	
	public synchronized static SinglePlayerManager getIntance(){
		if(manager==null){
			manager = new SinglePlayerManager();
		}
		return manager;
	}
	
	private  void initData(int num){		
		for(int i=0;i<num;i++){
			NewOggPlayer oggPlayer = new NewOggPlayer();
			playerQueue.offer(oggPlayer);
		}		
	}
	
	public synchronized NewOggPlayer pollOggPlayer(){
		if(playerQueue.size()==0){
			initData(2);
		}
		return playerQueue.pollFirst();
	}
	
	public void rebackOggPlayer(NewOggPlayer player){
		playerQueue.offer(player);
	}
	
	public synchronized DoReMiFa pollSound(){		
		return soundQueue.poll();
	}
	
	public synchronized void pushSound(DoReMiFa doremifa){
		soundQueue.offer(doremifa);
	}
	
	public void shutdown(){		
		while(playerQueue.size()!=0){
			NewOggPlayer player=playerQueue.poll();
			player.close();
		}
	}
	
	public synchronized int addSoundIndex(){
		soundIndex++;
		return soundIndex;
	}
	
}
